package com.example.arcapplication.ui;

import android.content.DialogInterface;
import android.graphics.Rect;
import android.view.View;

import com.example.arcapplication.R;
import com.example.arcapplication.base.CommonCallback;
import com.example.arcapplication.bean.MainCardBean;
import com.example.arcapplication.databinding.ActivityMainBinding;
import com.example.arcapplication.ui.adapter.MainCardAdapter;
import com.example.arcapplication.ui.view_model.MainActivityModel;
import com.example.arcapplication.util.DialogUtils;
import com.example.arcapplication.util.SharedUtils;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class MainActivity extends BaseActivity<ActivityMainBinding, MainActivityModel> {

    private MainCardAdapter mainCardAdapter;

    @Override
    protected Class<MainActivityModel> initViewModel() {
        return MainActivityModel.class;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        mBinding.recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
        mBinding.recyclerView.addItemDecoration(new MyRecyclerView());
        MainCardAdapter itemAdapter = new MainCardAdapter(getMainBeans());
        mBinding.recyclerView.setAdapter(itemAdapter);
        itemAdapter.setListener(new CommonCallback<Object>() {
            @Override
            public void onResult(Object... data) {
                MainCardBean itemBean = (MainCardBean) data[0];
                boolean downloadFinish = SharedUtils.isDownloadFinish();
                switch (itemBean.getText()) {
                    case "采购入库":
                        break;
                    case "材料出库":
                        break;
                    case "调拨入库":
                        break;
                    case "采购订单查询":
                        break;
                    case "实时库存":
                        break;
                    case "系统设置":
                        break;
                    default:
                        break;
                }
            }
        });
    }

    private List<MainCardBean> getMainBeans(){
        List<MainCardBean> beans= new ArrayList<>();
        beans.add(new MainCardBean("采购入库","1"));
        beans.add(new MainCardBean("材料出库","1"));
        beans.add(new MainCardBean("调拨入库","1"));
        beans.add(new MainCardBean("采购订单查询","1"));
        beans.add(new MainCardBean("实时库存","1"));
        beans.add(new MainCardBean("系统设置","1"));
        return beans;
    }

    private void showDialogInfo(String str) {
        DialogUtils.showInfoDialog(this, str, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                DialogUtils.closeInfoDialog();
            }
        });
    }

    private class MyRecyclerView extends RecyclerView.ItemDecoration {

        private final int space;

        public MyRecyclerView() {
            space = getResources().getDimensionPixelSize(R.dimen.layout_margin_10dp);
        }

        @Override
        public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
            super.getItemOffsets(outRect, view, parent, state);
            //由于每行都只有2个，所以第一个都是2的倍数，把左边距设为0
            if (parent.getChildLayoutPosition(view) % 2 == 0) {
                outRect.right = space;
                outRect.left = 0;
            } else {
                outRect.right = 0;
                outRect.left = space;
            }
        }
    }




}